package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.dto.AttributeTemplateNewDto;
import cn.tedu.csmall.product.pojo.dto.AttributeTemplateUpdateDto;
import cn.tedu.csmall.product.pojo.entity.PmsAttributeTemplate;
import cn.tedu.csmall.product.pojo.vo.*;
import cn.tedu.csmall.product.service.AttributeTemplateService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/attributeTemplate")
@Api(tags = "03. 属性模板管理")
public class AttributeTemplateController {
    @Autowired
    AttributeTemplateService service;

    @PostMapping("/addNew")
    @ApiOperation("添加模板")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> insert(@Valid AttributeTemplateNewDto attribute){
        PmsAttributeTemplate attributeTemplate = new PmsAttributeTemplate();
        BeanUtils.copyProperties(attribute, attributeTemplate);
        service.addNew(attribute);
        return JsonResult.ok();
    }

    @PostMapping("{id:[0-9]+}/update")
    @ApiOperation("根据ID修改相册")
    @ApiOperationSupport(order = 300)
    public JsonResult<Void> update(@Valid @Range(min = 1,message = "请提供正确的ID值")Long id, @Valid AttributeTemplateUpdateDto attributeTemplateUpdateDto){
        service.updateInfoById(id,attributeTemplateUpdateDto);
        return JsonResult.ok();
    }

    @GetMapping("")
    @ApiOperation("列表")
    @ApiOperationSupport(order = 420)
    public JsonResult<List<AttributeTemplateListItemVo>> list(){
        List<AttributeTemplateListItemVo> list = service.list();
        return JsonResult.ok(list);
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "相册ID", required = true, dataType = "long")
    public JsonResult<Void> delete(@PathVariable @Range(min = 1) Long id){
        service.delete(id);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询相册详情")
    @ApiOperationSupport(order = 410)
    public JsonResult<AttributeTemplateStandardVo> getStandardById(
            @PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id) {
        AttributeTemplateStandardVo queryResult = service.getStandardById(id);
        return JsonResult.ok(queryResult);
    }


}
